package com.rimi.controller;

import com.rimi.bean.News;
import com.rimi.bean.User;
import com.rimi.commons.CommonResult;
import com.rimi.commons.ResultStatus;
import com.rimi.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liHaiYang
 * @version V1.0
 * @Description: (用一句话描述该文件做什么)
 * @date 2021/7/9 15:11
 */
@RestController
@CrossOrigin
@Api(description = "新闻收藏",tags = "新闻收藏")
public class NewsController {
    @Autowired
    NewsService newsService;

    @PostMapping("news/add/{newsId}")
    @ResponseBody
    @ApiOperation("添加新闻收藏")
    public CommonResult add(@PathVariable Integer newsId){
        //获取当前用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if(user == null){
            return CommonResult.onFailed(ResultStatus.PLEASE_LOGIN);
        }else {
            int row = newsService.addNewsByUserId(newsId,user.getUserId());
            return row == 1? CommonResult.onSuccess(null):CommonResult.onFailed(ResultStatus.ADD_FAILED);
        }
    }

    @GetMapping("news/show")
    @ResponseBody
    @ApiOperation("展示用户新闻收藏")
    public CommonResult<List<News>> show(){
        //获取当前用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if(user == null){
            return CommonResult.onFailed(ResultStatus.PLEASE_LOGIN);
        }else {
       List<News> newses = newsService.showNewsByUserId(user.getUserId());
       if(newses != null){
           return CommonResult.onSuccess(newses);
       }
        }
        return CommonResult.onFailed(ResultStatus.SHOW_FAILED);
    }

    @DeleteMapping("news/delete/{newsId}")
    @ResponseBody
    @ApiOperation("取消收藏的新闻")
    public CommonResult delete(@PathVariable Integer newsId){
       return newsService.deleteNewsByNewsId(newsId);
    }

}
